package com.ruoyi.web.sso.common.exception;

import com.ruoyi.web.sso.common.util.BusinessException;
import com.ruoyi.web.sso.common.util.ErrorCode;
import com.ruoyi.web.sso.domain.DTO.StatusResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * *******************************
 * Created by Ron.
 * form com.tuo51
 * on 2020/12/30
 * *******************************

 */
@ControllerAdvice
@Slf4j
public class GlobalValidationExceptionHandler {

    @ExceptionHandler(BindException.class)
    @ResponseBody
    public StatusResponse handle(BindException exception) {
        exception.printStackTrace();
        return StatusResponse.error(exception.getTarget().toString(),  "登录已过期，请重新登录" );
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public StatusResponse bizExceptionHandle(BusinessException exception) {
        exception.printStackTrace();
        return StatusResponse.error(exception.getErrorCode(), exception.getMessage());
    }
}